﻿/*
 * @author: wizardc
 */

using System.IO;
using System.Linq;
using DouEditor.Utils;
using UnityEditor;
using UnityEditor.ShortcutManagement;
using UnityEngine;

namespace DouEditor.Extend
{
    public sealed class ShortcutMgr
    {
        private static ShortcutMgr _instance;

        public static ShortcutMgr instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ShortcutMgr();
                    _instance.MakeSafelyProfiler();
                    _instance.LoadShortcuts();
                }
                return _instance;
            }
        }

        private const string DataFilePath = "Assets/DouFramework/Editor/Extend/Shortcut/shortcut.asset";

        internal ShortcutData _shortcutData;

        private ShortcutMgr()
        {
        }

        public void MakeSafelyProfiler()
        {
            var mgr = ShortcutManager.instance;
            if (mgr.IsProfileReadOnly(mgr.activeProfileId))
            {
                foreach (var profileId in mgr.GetAvailableProfileIds())
                {
                    if (!mgr.IsProfileReadOnly(profileId))
                    {
                        mgr.activeProfileId = profileId;
                        return;
                    }
                }
                mgr.CreateProfile("Custom Shortcut");
                mgr.activeProfileId = "Custom Shortcut";
            }
        }

        public void RegisterShortcut(string id, string category, string name, string desc, KeyCode keyCode = KeyCode.None, bool control = false, bool shift = false, bool alt = false)
        {
            if (_shortcutData.shortcuts.All(item => item.id != id))
            {
                var shortcut = new ShortcutConfig(id, category, name, desc, keyCode, control, shift, alt);
                _shortcutData.shortcuts.Add(shortcut);
                ApplyShortcut(shortcut);
            }
        }

        public ShortcutConfig GetShortcut(string id)
        {
            return _shortcutData.shortcuts.Find(s => s.id == id);
        }
        
        public void ApplyShortcut(ShortcutConfig shortcut)
        {
            MakeSafelyProfiler();
            
            if (shortcut.keyCode == KeyCode.None)
            {
                ShortcutManager.instance.RebindShortcut(shortcut.id, ShortcutBinding.empty);
                return;
            }
            
            var modify = ShortcutModifiers.None;
            if (shortcut.control)
            {
                modify |= ShortcutModifiers.Control;
            }
            if (shortcut.shift)
            {
                modify |= ShortcutModifiers.Shift;
            }
            if (shortcut.alt)
            {
                modify |= ShortcutModifiers.Alt;
            }
            
            ShortcutManager.instance.RebindShortcut(shortcut.id, new ShortcutBinding(new KeyCombination(shortcut.keyCode, modify)));
        }

        public void LoadShortcuts()
        {
            if (File.Exists(DataFilePath))
            {
                _shortcutData = AssetDatabase.LoadAssetAtPath<ShortcutData>(DataFilePath);
            }
            else
            {
                _shortcutData = new ShortcutData();
            }
        }

        public void SaveShortcuts()
        {
            if (File.Exists(DataFilePath))
            {
                EditorUtility.SetDirty(_shortcutData);
                AssetDatabase.SaveAssetIfDirty(_shortcutData);
            }
            else
            {
                EditorUtil.CreateDirByFilePath(DataFilePath);
                AssetDatabase.CreateAsset(_shortcutData, DataFilePath);
                AssetDatabase.Refresh();
            }
        }
    }
}
